<template>
<div class="app-container">
    <!-- 输入表单 -->
    <el-form label-width="120px">
        <el-form-item label="讲师名称">
            <el-input v-model="teacher.name" />
        </el-form-item>
        <el-form-item label="入驻时间">
            <el-date-picker v-model="teacher.joinDate" value-format="yyyy-MM-dd" />
        </el-form-item>
        <el-form-item label="讲师排序">
            <el-input-number v-model="teacher.sort" :min="0" />
        </el-form-item>
        <el-form-item label="讲师头衔">
            <el-select v-model="teacher.level">
                <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
            -->
                <el-option :value="1" label="高级讲师" />
                <el-option :value="2" label="首席讲师" />
            </el-select>
        </el-form-item>
        <el-form-item label="讲师资历">
            <el-input v-model="teacher.career" />
        </el-form-item>
        <el-form-item label="讲师简介">
            <el-input v-model="teacher.intro" :rows="10" type="textarea" />
        </el-form-item>

        <!-- 讲师头像：TODO -->
        <el-form-item label="讲师头像">
            <el-upload class="avatar-uploader" :action="BASE_API + '/admin/oss/file/upload?module=admin'" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <img v-if="teacher.avatar" :src="teacher.avatar" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
        </el-form-item>

        <el-form-item>
            <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate()">保存</el-button>
        </el-form-item>
    </el-form>
</div>
</template>

<script>
import teacherApi from "@/api/teacher";
export default {
    name: "",
    props: {},
    components: {},
    data() {
        return {
            teacher: {
                sort: 0,
                level: 1,
            },
            saveBtnDisabled: false,
            updateOrsave: true,
            BASE_API: process.env.BASE_API,
        };
    },
    computed: {},
    watch: {},
    methods: {
        //保存点击事件
        saveOrUpdate() {
            this.saveBtnDisabled = true;
            if (this.updateOrsave) {
                //新增
                this.saveDate();
            } else {
                //修改
                this.updateDate();
            }
        },
        //新增讲师
        saveDate() {
            teacherApi.save(this.teacher).then((req, rep) => {
                if (req.code == 20000) {
                    this.$message({
                        message: req.message,
                        type: "success",
                    });
                    this.$router.push({
                        path: "/teacher/list",
                    });
                } else {
                    this.$message.error(req.message);
                    this.saveBtnDisabled = false;
                }
            });
        },
        //修改回显讲师数据
        handleEdit(id) {
            teacherApi.getByIdOne(id).then((req, rep) => {
                this.teacher = req.data.data;
            });
        },
        //修改讲师
        updateDate() {
            teacherApi.update(this.teacher).then((req, rep) => {
                if (req.code == 20000) {
                    this.$message({
                        message: req.message,
                        type: "success",
                    });
                    this.$router.push({
                        path: "/teacher/list",
                    });
                } else {
                    this.$message.error(req.message);
                }
            });
        },
        //文件上传前校验
        beforeAvatarUpload(file) {
            //是否为JPG,PNG图片
            const isJSP = file.type === "image/jpeg";
            const isPNG = file.type === "image/png";
            //是否小于2M
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJSP && !isPNG) {
                this.$message.error("上传头像图片只能是JPG或者PNG格式！");
            }
            if (!isLt2M) {
                this.$message.error("上传头像图片的大小不能超过2MB！");
            }
            return isPNG || (isJSP && isLt2M);
        },
        //文件上传成功的回调
        handleAvatarSuccess(req, file, fileList) {
            if (req.code == 20000) {
                this.$message({
                    message: "头像图片上传成功！",
                    type: "success",
                });
                this.teacher.avatar = req.data.url;
                //强制重新渲染
                this.$forceUpdate();
            } else {
                this.$message.error("头像图片上传失败！");
            }
        },
    },
    created() {
        if (this.$route.params.id != null) {
            this.handleEdit(this.$route.params.id);
            this.updateOrsave = false; //修改
        }
    },
    mounted() {},
    beforeDestroy() {},
};
</script>

<style>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
